<?php

namespace App\Http\Controllers\Api\V1\Orders;

use Illuminate\Support\Facades\Auth;
use App\Models\Orders\Order;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Api\V1\BaseController;
use Illuminate\Support\Facades\Validator;
use App\Models\Goods;
use App\Models\User\Users;

class OrderController extends BaseController
{
    /**
     * 下订单
     * @param Request $request
     * @return mixed
     */
    public function placeOrder (Request $request){
        $data = $request->all();
        $price = Goods::where(['id'=>$request->goods_id])->value('price');
        $data['payed'] = $request->num * $price;
        $data['user_id'] = Auth::user() ? Auth::user()->id : 0;
        $validate = $this->validateRequest($data);
        if ($validate->fails()){
            $warnings = $validate->messages();
            return $this->returnValidate($warnings->first());
        }else{

            try {
                $order = new Order();

                $info = $order->insertRow($data);


            } catch (\Exception $e) {
                //记录失败原因
                \Log::info('兑换接口placeOrder信息：'.$e->getMessage());
                return $this->returnArray('',500,'内部错误');
            }
        }
        return $this->returnArray(['id'=>$info->id,'sn'=>$info->sn]);

    }
    /**
     * 查询可用金币
     * #user.getStatic.rewardNum
     */
    /**
     * 如果指定id,返回指定集合
     * 如果没指定id,返回用户最新一条
     * @param mixed $request
     * 查询一条订单信息
     */
    public function getUserOrder (Request $request){
        $order = new Order();
        $info = [];
        try{
            $request->user_id = \Auth::user() ? \Auth::user()->id : 0;

            $info = $order->getRow($request);

            list($info['receiver_name'],$info['receiver_address'],$info['receiver_phone'],) = explode(',',$info['consignee']);

        }catch (\Exception $e){
            \Log::info('getUserOrder接口错误消息：'. $e->getMessage());
        }
        return $this->returnArray($info);

    }
    /**
     * 下单成功页面收货人信息
     * #user.getAddressRow
     */
    /**
     * 验证request数据
     * @param array $data
     * @return mixed $validate
     */
    protected function validateRequest ($data) {

        $reward = Users::where(['id'=>$data['user_id']])->value('reward');
        $validate = Validator::make($data, [
            'goods_id' => ['required',Rule::exists('goods','id')],
            // 'user_id' => ['required',Rule::exists('users','id')],
            'consignee' => 'required',

            'num' => ['required','integer'],
            //金币不足#回掉支持
            'payed' => ['max:'.$reward],
        ],[
            'goods_id.required' => '请选择兑换商品',
            'goods_id.exists' => '商品不存在',
            // 'user_id.required' => '非法操作',
            // 'user_id.exists' => '用户不存在',

            'consignee.required' => '请填写收货地址',
            'num.required' => '请选择数量',
            'num.integer' => '数量格式不正确',
            'payed.max' => '金币不足'
        ]);
        return $validate;
    }
}
